(function($){

	var form = 'form.paylog_form';
	var calenderAppended = false;

	var getPosition = function(id) {
		var x = parseInt(id.replace(/box_(\d+)_\d+/, '$1'));
		var y = parseInt(id.replace(/box_\d+_(\d+)/, '$1'));
		return {'x' : x, 'y' : y};
	}

	var getIdByPosition = function(x,y) {
		return 'box_'+x+'_'+y;
	}

	var addNewLine = function(callback) {

		var last_tr = $(form + ' tr:last');
		last_tr.clone().insertAfter(last_tr);

		var last_tr = $(form + ' tr:last');

		var newIncrement = parseInt(last_tr.find('td:first').text()) + 1;
		last_tr.find('td:first').text(newIncrement);

		last_tr.find('input, select').each(function(){
			var pos = getPosition($(this).attr('id'));
			var newId = getIdByPosition(pos.x, pos.y + 1);
			var newName = $(this).attr('name').replace(/^items\[\d*\]/, 'items['+newIncrement+']');

			$(this).attr('id', newId);
			$(this).attr('name', newName);
			if ( newName.match(/(price|name)/) ) {
				$(this).val('');
			} else {
				var oldId = getIdByPosition(pos.x, pos.y);
				var oldVal = $('[id='+oldId+']').val();
				$(this).val(oldVal);
			}
			$(this).paylogInput();
		});

		last_tr.toggleClass('odd');
		last_tr.toggleClass('even');

		if ( callback != undefined ) {
			callback();
		}
	}

	var apendCalender = function() {
		$(form + ' input[name*=date]').datepicker({
			dateFormat: 'yy-mm-dd',
			altFormat: 'yy-mm-dd',
			beforeShow: function(){ $('#ui-datepicker-div').css('z-index', '99999'); },
 		});
	}

	var removeCalender = function() {
		$(form + ' input[name*=date]').datepicker('destroy');
	}

	$.fn.paylogInput = function() {
		return this.each(function() {
		
			if ( !calenderAppended ) {
				apendCalender();
				calenderAppended = true;
			}

			var tagName = this.tagName.toLowerCase();
			if ( tagName == 'select' ) {
				$(this).keydown(function(e) {
					if ( e.keyCode  == 13 || e.keyCode == 37 || e.keyCode == 39 ) {
						var id = $(this).attr('id');
						var pos = getPosition(id);

						if ( e.keyCode == 37 ) {
							pos.x--;
						} else if ( e.keyCode == 39 ) {
							pos.x++;
						} else if ( e.keyCode == 13 ) {
							pos.x++;
						}

						var next = '#'+getIdByPosition(pos.x, pos.y);

						if ( $(next).length ) {
							$(next).focus();
						}

						return false;
					}
				});
			} else if ( tagName == 'input' ) {
				$(this).keydown(function(e) {
					if ( 37 <= e.keyCode && e.keyCode <= 40 ) {
						var id = $(this).attr('id');
						var pos = getPosition(id);

						if ( e.keyCode == 37 ) {
							pos.x--;
						} else if ( e.keyCode == 38 ) {
							pos.y--;
						} else if ( e.keyCode == 39 ) {
							pos.x++;
						} else if ( e.keyCode == 40 ) {
							pos.y++;
						}

						$(this).datepicker('hide');

						var next = '#'+getIdByPosition(pos.x, pos.y);

						if ( $(next).length ) {
							$(next).focus();
							$(this).blur();
						} else if ( e.keyCode == 40 ) {

							if ( $(form + ' tr:last td:first').text() >= 50 ) {
								return;
							}

							removeCalender();
							addNewLine(function(){
								var next = '#'+getIdByPosition(pos.x, pos.y);
								$(next).focus();
								apendCalender();
							});
							$(this).blur();
						}
					}
				});

				$(this).keypress(function(e){
              		return (e.which != 13);
				});

				if ( $(this).attr('name').match(/price/) )
				{
					$(this).blur(function(e){
						var value = $(this).val();
						var str = '';
						for (var i = 0; i < value.length; ++i) {
							var code = value.charCodeAt(i);
							var char = value.charAt(i);
							if ( 65296 <= code && code <= 65305 ) {
								str += (code - 65296);
							} else if ( 0 <= char && char <= 9 ) {
								str += char;
							}
						}
						$(this).val(str);
					});
				}
			}
		});
	};
})(jQuery);

jQuery(function($) {

	var unloadOk = false;

	if ( $('form.paylog_form').length )
	{
		$(window).bind('beforeunload', function() {
			if ( !unloadOk ) {
				return "Are you sure to leave this page?";
			}
		});

		$('form.paylog_form').submit(function(){
			unloadOk = true;
		});

		$('form.paylog_form tr:gt(1)').each(function(){
			var price = $(this).find('input[name*="price"]').val();
			var name  = $(this).find('input[name*="name"]').val();
			if ( !price && !name ) {
				$(this).remove();
			}
		});
	}

	if ( $('form.paylog_form select').length ) {
		$('form.paylog_form select').paylogInput();
	}

	if ( $('form.paylog_form input').length ) {
		$('form.paylog_form input').paylogInput();
	}

	if ( $(".paylog form.dateNavi input[type=text]").length ) {
		$(".paylog form.dateNavi input[type=text]").datepicker({
			dateFormat: 'yy-mm-dd',
			altFormat: 'yy-mm-dd',
			beforeShow: function(){ $('#ui-datepicker-div').css('z-index', '99999'); },
 		});
	}
});

jQuery(function($) {
	$('.paylog input.button').each(function() {
		$(this).wrap('<span class="button"><span></span></span>');
		$(this).removeClass('button');
	});
	$('.paylog input.minibutton').each(function() {
		$(this).wrap('<span class="button minibutton"><span></span></span>');
		$(this).removeClass('minibutton');
	});
});